//
//  HotViewController.m
//  ZAKER
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HotViewController.h"


@interface HotViewController ()<UITableViewDelegate, UITableViewDataSource>

{
    
    NSInteger _pageCount;
    
}

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *allArray; // 存放所有字典数据
@property (nonatomic, retain) NSMutableArray *tableArray; // 存放的是所有tableArray要用到的数据
@property (nonatomic, retain)MBProgressHUD *mb;


@end


@implementation HotViewController

- (void)dealloc
{
    
    [_tableArray release];
    [_allArray release];
    [_tableView release];
    [_mb release];
    
    [super dealloc];
    
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        _pageCount = 1;
    }
    
    return self;
    
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = RGBACOLOR(240, 51, 51, 1);
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 37.5 * myWidth, 10 * myHeight, 75 * myWidth, 40 * myHeight)];
    label.text = @"热点";
    label.textAlignment = 1;
    self.navigationItem.titleView = label;
    [label release];
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    _mb = [[MBProgressHUD alloc] initWithView:window];
    [self.view addSubview:_mb];
    _mb.labelText = @"正在加载";
    [_mb show:YES];

    [self myConnection];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)8 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        if (_mb != nil) {
            
            [_mb removeFromSuperview];
            
            UIAlertController *a = [UIAlertController alertControllerWithTitle:@"提示" message:@"数据加载失败" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            [a addAction:sure];
            // 模态推出
            [self presentViewController:a animated:YES completion:^{
                
            }];
            
        }
    });
}

#pragma mark - 网络解析

- (void)myConnection
{

    NSString *str = [NSString stringWithFormat:Hot, @(_pageCount)];
    
// 第三方网络请求
    [MyAFNetworking GetWithURL:str dic:nil data:^(id responsder) {
    NSDictionary *dic = responsder[0];
    NSArray *itemArray = dic[@"item"];
        
    for (NSDictionary *smallDic in itemArray) {
        
        HotModel *model = [[HotModel alloc] init];
        [model setValuesForKeysWithDictionary:smallDic];
        
        if (![model.type isEqualToString:@"video"]) {
            
            [self.tableArray addObject:model];
            [model release];
            
        }
    }
        
    [self createTableView];
        
    [_mb hide:YES];
    _mb = nil;

}];
}

#pragma mark - 创建tableView, 以及实现协议方法

-(void)createTableView
{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 * myHeight)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // 刷新
    [self addHeader];
    [self addFooter];
    
    [self.view addSubview:_tableView];
    [_tableView release];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.tableArray count];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_tableArray.count != 0) {
        
    HotModel *model = [self.tableArray objectAtIndex:indexPath.row];
        
    // 第一种样式:照片和标题
    if ( [model.type isEqualToString:@"text_live"] || [model.type isEqualToString:@"topic2"]) {
        
        static NSString *cellIdentify = @"normalCell";
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        
        if (!cell) {
            
            cell = [[[NormalTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setValuesWithModel:model];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
       return cell;
        
    }
        
    // 第二种样式
    if ([model.type isEqualToString:@"doc" ]) {
        
        static NSString *cellIdentify = @"docCell";
        DocTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        
        if (!cell) {
            
            cell = [[[DocTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
            
        }
        
        [cell setValuesWithModel:model];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    
    // 第三种样式:三张照片
    if ([model.type isEqualToString:@"slide"]) {
        
        static NSString *cellIdentify = @"slideCell";
        SlideTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        
        if (!cell) {
            
            cell = [[[SlideTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
            
        }
        
        [cell setValuesWityArray:_tableArray[indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    }
    
    return [[UITableViewCell alloc] init];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 120 * myHeight;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HotDetailViewController *hot = [[HotDetailViewController alloc]init];
    HotModel *model = [_tableArray objectAtIndex:indexPath.row];
    
    NSString *str = model.commentsUrl;
    [hot sendWithUrl:[NSURL URLWithString:str]];
    [self.navigationController pushViewController:hot animated:YES];
    [hot release];
    
}

- (void)addHeader
{
    [self.tableView addHeaderWithCallback:^{
        
        [_tableArray removeAllObjects];
        [self myConnection];
        [_tableView headerEndRefreshing];
        
    }];
}

#pragma mark - 加载和刷新

- (void)addFooter
{
    
    [self.tableView addFooterWithCallback:^{
        
        _pageCount++;
        [self myConnection];
        [_tableView footerEndRefreshing];
        
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
